'use strict';

const await = require('await-stream-ready/lib/await');

const Service = require('egg').Service;

class ScoreService extends Service {
  async searchScore(username, exam_id) {
    const { ctx, app } = this
    let user = await app.model.User.findOne({
      where: {
        username
      }
    })
    user = JSON.parse(JSON.stringify(user))
    console.log(user)
    
    let score = await app.model.Score.findAll({
      where:{
        // branch_id,
        exam_id,
        user_id:user.id
      },
    });
    if(score.length === 0){
      return null
    }
    score = JSON.parse(JSON.stringify(score))
    console.log(score)
    let classes = await app.model.Class.findOne({
      where:{
        id: score[0].class_id
      },
    })
    let branch = await app.model.Branch.findOne({
      where:{
        id: score[0].branch_id
      },
    })
    let exam = await app.model.Exam.findOne({
      where:{
        id: score[0].exam_id
      },
    })
    score[0].class = JSON.parse(JSON.stringify(classes))
    score[0].exam = JSON.parse(JSON.stringify(exam))
    score[0].branch = JSON.parse(JSON.stringify(branch))
    score[0].user = user
    let result ={
      score
    } 
    return result ? result : null;
  }
}

module.exports = ScoreService;
